package Server.Database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Paulius Dambrauskas
 */
public class UsersRegister {

    private Database db;

    public UsersRegister(Database db) {
        this.db = db;
    }

    public boolean login(String username, String password) throws SQLException {

        Boolean result = false;
        //System.out.print("Jungiasi");
        db.resultSet = db.statement.executeQuery("SELECT * FROM user WHERE Username = '" + username + "' and Password = '" + password + "'");
        if (db.resultSet.next()) {
            result = true;
        }

        return result;
    }

    public boolean isAdmin(String username) throws SQLException {
        db.resultSet = db.statement.executeQuery("SELECT Admin FROM user WHERE Username = '" + username + "'");
        db.resultSet.next();
        if (db.resultSet.getInt(1) == 1) {
            return true;
        } else {
            return false;
        }
    }

    public ArrayList<String> getAllUsers() throws SQLException {

        ArrayList<String> result = new ArrayList<String>();

        db.resultSet = db.statement.executeQuery("SELECT * from user");

        while (db.resultSet.next()) {

            result.add(db.resultSet.getString(2));

        }

        return result;

    }

    public void deleteUser(String name) throws SQLException, IllegalArgumentException {
        //statement.execute("DELETE FROM depends WHERE user=(SELECT User_ID FROM user WHERE Username='"+name+"' LIMIT 1)");
        db.resultSet = db.statement.executeQuery("SELECT Admin from user WHERE Username='" + name + "' LIMIT 1");
        db.resultSet.next();
        if (db.resultSet.getInt("Admin") == 0) {
            db.statement.execute("DELETE FROM depends WHERE user='" + name + "'");
            db.statement.execute("DELETE FROM user WHERE Username='" + name + "' LIMIT 1");
        } else {
            throw new IllegalArgumentException();
        }

    }

    public void blockUser(String name, String reason) throws SQLException {
        db.statement.executeUpdate("UPDATE user SET Blocked = 1, Block_Reason = '" + reason + "' WHERE Username = '" + name + "'");
    }

    public void unblockUser(String name) throws SQLException {
        db.statement.executeUpdate("UPDATE user SET Blocked = 0, Block_reason = ' ' WHERE Username = '" + name + "'");
    }

    public void addUser(String name, String pwd, ArrayList<String> projects) throws Exception {
        if (name.isEmpty() || pwd.isEmpty() || userExists(name) || projects.size() == 0) {
            throw new IllegalArgumentException();
        } else {
            //Sukuria useri
            db.statement.executeUpdate("INSERT INTO user (Username, Password)VALUES('" + name + "', '" + pwd + "')");
            //Sudeda dependensus
               /* resultSet = statement.executeQuery("SELECT User_ID FROM user WHERE Username='"+name+"' LIMIT 1");
             String user = "";
             while(resultSet.next()){
             user = resultSet.getString(1);
             }
             System.out.println(user);*/
            for (int i = 0; i < projects.size(); i++) {
                //ŽIAURIAI optimaliai rišam pagal id, nors ir pavadinimai unikalūs... :D
                // statement.executeUpdate("INSERT INTO depends (User, Project)VALUES('"+user+"', (SELECT ProjectID FROM projects WHERE ProjectName='"+projects.get(i)+"' LIMIT 1))");
                db.statement.executeUpdate("INSERT INTO depends (User, Project)VALUES('" + name + "', '" + projects.get(i) + "')");
            }
        }
    }

    private boolean userExists(String name) throws SQLException {
        db.resultSet = db.statement.executeQuery("SELECT Username FROM user WHERE Username='" + name + "'"); //tarkim nebus vardas su " simboliu :D
        return db.resultSet.next();
    }

    public Boolean isUserBlocked(String name) {
        Boolean isBlocked = false;
        int rezult = 0;
        try {
            db.resultSet = db.statement.executeQuery("SELECT Blocked FROM user WHERE Username = '" + name + "'");
            db.resultSet.next();
            rezult = db.resultSet.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (rezult == 1) {
            isBlocked = true;
        }

        return isBlocked;
    }

    public ArrayList<String> getBlockedUsersAndReasons() throws SQLException {

        ArrayList<String> result = new ArrayList<String>();

        db.resultSet = db.statement.executeQuery("SELECT Username, Block_Reason FROM user WHERE Blocked = 1");

        while (db.resultSet.next()) {
            result.add(db.resultSet.getString(1) + " : " + db.resultSet.getString(2));
        }

        return result;
    }
}
